package com.example.dangerouscargodriver.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.controller.BankAccountInfoController;
import com.example.dangerouscargodriver.controller.EnterPlatformInfoController;
import com.example.dangerouscargodriver.entry.BankAccount;
import com.example.dangerouscargodriver.entry.EmployeeInfo;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.utils.TextUtils;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.example.dangerouscargodriver.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

public class BindingPersonalCardActivity extends HttpRequestActivity implements View.OnClickListener {

    private static final String TAG = "PersonalCardActivity";
    private static final int BINDING_PERSONAL_CARD_TYPE = 2;

    private ActionBar mActionBar = null;
    private TextView mProtocol = null;

    private EditText mCardUser = null;
    private EditText mCardNo = null;
    private EditText mPersonNo = null;
    private EditText mTelephoneNo = null;
    private EditText mVerifyNo = null;
    private Button mBindAction = null;
    private boolean mProtocolChecked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(BindingPersonalCardActivity.class);
        setContentView(R.layout.activity_binding_personal_card);

        mCardUser = findViewById(R.id.ed_card_user);
        mCardNo = findViewById(R.id.ed_card_no);
        mPersonNo = findViewById(R.id.ed_identity_person_no);
        mTelephoneNo = findViewById(R.id.ed_telephone_no);
        mVerifyNo = findViewById(R.id.ed_verify_no);

        mBindAction = findViewById(R.id.btn_action_bind_card);
        mBindAction.setOnClickListener(this);

        mProtocol = findViewById(R.id.tv_protocol);

        TextUtils.getBuilder().click(getResources().getString(R.string.company_partner_protocol), getResources().getColor(R.color.liji_c_blue), new TextUtils.OnClickListener() {
            @Override
            public void onClick(int position) {
                switch (position){
                    case 0:
                        //跳转链接
                        //ToastUtils.showShortToast(BindingCardActivity.this, "0");
                        Intent intent = new Intent(BindingPersonalCardActivity.this, ShowHtmlActivity.class);
                        intent.putExtra("type", "3");
                        startActivity(intent);
                        break;
                    case 1:
                    ToastUtils.showShortToast(BindingPersonalCardActivity.this, "1");
                        break;
                }
            }
        }, "《危司机用户服务协议》", "《电子签约数字证书用户使用须知》").checkBox(this,  mProtocol, new TextUtils.OnImageClickListener() {
            @Override
            public void onChecked() {
                mProtocolChecked = true;
            }

            @Override
            public void onUnChecked() {
                mProtocolChecked = false;
            }
        }).clickInto(mProtocol);

        mActionBar = findViewById(R.id.actionBarBindPersonalCard);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private boolean checkUserInput(){
        if(mCardUser.getText().toString().equals("") ||
                mCardNo.getText().toString().equals("") ||
                mTelephoneNo.getText().toString().equals("") ||
                mPersonNo.getText().toString().equals("") ||
                mVerifyNo.getText().toString().equals("") ||
                !mProtocolChecked){
            return false;
        }
        else{
            return true;
        }
    }

    private void executeBindCard(){
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("token", AuthController.Instance().getToken());

        HashMap<String, String> param = new HashMap<String, String>();
        param.put("bank_acct_type", Integer.toString(BINDING_PERSONAL_CARD_TYPE));
        param.put("card_name", mCardUser.getText().toString());
        param.put("card_num", mCardNo.getText().toString());
        param.put("cert_id", mPersonNo.getText().toString());
        param.put("tel_no", mTelephoneNo.getText().toString());
        param.put("code", mVerifyNo.getText().toString());

        sendHttpRequest(
                IHttpRequest.RequestMethod.POST_METHOD,
                RemoteAPI.REQUEST_V1_USER_BANK_ACCOUNT,
                header,
                param,
                RemoteAPICmd.REQUEST_V1_USER_BANK_ACCOUNT);
    }

    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            if(actionId == RemoteAPICmd.REQUEST_V1_USER_BANK_ACCOUNT){
                Log.d(TAG, "onResponse: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    ToastUtils.showLongToast(this, "绑定成功!");
                    finish();
                }
                else{
                    ToastUtils.showLongToast(this, "绑定失败!");
                }
                BankAccountInfoController.Instance().updateBindingInfo();
            }
            else{

            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {
        if(actionId == RemoteAPICmd.REQUEST_V1_USER_BANK_ACCOUNT){
            ToastUtils.showLongToast(this, "请求失败，请稍后重试!");
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_action_bind_card){
            if(checkUserInput()) {
                executeBindCard();
            }
            else{
                ToastUtils.showLongToast(this, "请填写完整信息");
            }
        }
    }
}